package com.tensquare.friend.controller;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description
 * @author: xu1
 **/

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    //addfanscountandfollowcount()：updateFanscountByUserid 和updateFollowcountByFriendid
    @Autowired
    private UserClient userClient;

    /**
     * （根据type种类）添加好友或者非好友
     * @param friendid 是主键
     * @param type     也是主键     联合主键
     * @return
     */
    @RequestMapping(value = "/like/{friendid}/{type}", method = RequestMethod.PUT)
    public Result findById(@PathVariable String friendid,@PathVariable String type) {//接受参数，也可以用int来，反正都是自动转换
        //验证是否登录，并且拿到当前登录的用户id
        Claims claims= (Claims) request.getAttribute("claims_user");
        //claims可以获取到 令牌里 存的键值对信息
        if(claims==null){//claims不会为 ""  空串
            //说明当前用户没有 user 这个角色
            return new Result(false, StatusCode.ACCESSERROR,"权限不足");
        }
        //???
        String userid = claims.getId();
        System.out.println("userid---"+userid);

        //判断是添加好友还是 添加非好友
        System.out.println("type---"+type);
        if(type!=null){
            if(type.equals("1")){
                //添加好友
                int flag = friendService.addFriend(userid,friendid);
                System.out.println("friendflag---"+flag);
                if(flag==0){
                    return new Result(false,StatusCode.ERROR,"不能重复添加好友");
                }
                if(flag==1){
                    userClient.addfanscountandfollowcount(1, userid, friendid);
                    return new Result(true,StatusCode.OK,"好友添加成功");
                }
            }else if(type.equals("2")) {
                //添加非好友
                int flag = friendService.addNoFriend(userid,friendid);
                System.out.println("nofriendflag---"+flag);
                if(flag==0){
                    return new Result(false,StatusCode.ERROR,"不能重复添加好友");
                }
                if(flag==1){
                    return new Result(true,StatusCode.OK,"非好友添加成功");
                }
            }
            return new Result(false,StatusCode.ERROR,"参数异常");//type参数异常
        }
        return new Result(false,StatusCode.ERROR,"参数异常");//type参数异常

    }

    @RequestMapping(value = "/{friendid}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String friendid) {
        //验证是否登录，并且拿到当前登录的用户id
        Claims claims= (Claims) request.getAttribute("claims_user");
        //claims可以获取到 令牌里 存的键值对信息
        if(claims==null){//claims不会为 ""  空串
            //说明当前用户没有 user 这个角色
            return new Result(false, StatusCode.ACCESSERROR,"权限不足");
        }
        String userid = claims.getId();
        System.out.println("userid---"+userid);

        //应该是 页面先 显示出 能删除的好友列表

        //这不考虑  删除会失败的情况和重复删除的东西

        friendService.deleteFriend(userid,friendid);
        userClient.addfanscountandfollowcount(-1, userid, friendid);
        return new Result(true, StatusCode.OK,"删除成功");
    }



}
